﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Program_Linq2
{

    public class User
    {
        public Guid Id { get; set; }
        public int Number { get; set; }

        public override string ToString()
        {
            return $"ID:{Id}，\n\r Inde:{Number}\n\r";
        }
    }

    public class UserInfo
    {
        public Guid UserId { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public double Height { get; set; }
        public double Weight { get; set; }
        public string Introduce { get; set; }

        public override string ToString()
        {
            return $"Name:{Name}\n\r" +
                   $"Age：{Age}\n\r" +
                   $"Height:{Height}\n\r" +
                   $"Weight:{Weight}\n\r" +
                   $"Introduce:{Introduce}\n\r";
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var users = new List<User>();
            for (int i = 0; i < 4; i++)
            {
                users.Add(new User
                {
                    Id = Guid.NewGuid(),
                    Number = i
                });
            }

            var userInfos = new List<UserInfo>();
            users.ForEach(item =>
            {
                userInfos.Add(
                        new UserInfo()
                        {
                            UserId = item.Id,
                            Name = "test" + item.Number,
                            Age = new Random().Next(15,24),
                            Height = new Random().Next(150,200),
                            Weight = new Random().Next(60,300),
                            Introduce = "测试数据:"+item.Number
                        }
                    );
            });

            var LinqResult = from user in users
                             join userinfo in userInfos
                             on user.Id equals userinfo.UserId
                             where userinfo.Age < 20
                             select new { user, userinfo };

            var LambdaResult = userInfos.Where(x => x.Age < 20).Select(item => new
                {
                    user = users.FirstOrDefault(x => x.Id == item.UserId),
                    userInfo = item
                }
            );

            Console.WriteLine("LinqResult >> " + LinqResult.Count()) ;
            LinqResult.ToList().ForEach(item =>
            {
                Console.WriteLine(item.user.ToString() + item.userinfo.ToString());
            });

            Console.WriteLine("LambdaResult >> " + LambdaResult.Count());
            LambdaResult.ToList().ForEach(item =>
            {
                Console.WriteLine(item.user.ToString() + item.userInfo.ToString());
            });

            Console.ReadLine();
        }
    }
}
